<?php
	if(!isset($_SESSION['member_is_admin']) || $_SESSION['member_is_admin'] == ''){
		header("Location: index.php");
	}
	
	//--
	$xtpl_main->assign ( "page_title", "Movie Task" );
	
	//--
	$movieStatusIsChecked = true;
	$frmActValue = 'doMovieSave';
	
	if(isset($_GET['movie_id'])){
		$resultFetchMovieInformation = mysql_query("SELECT * FROM movie WHERE movie_id = '".$_GET['movie_id']."'");
		if(mysql_num_rows($resultFetchMovieInformation) < 1){
			$xtpl->parse('center.movie_task_error');
		}
		else {
			$rowFetchMovieInformation = mysql_fetch_array($resultFetchMovieInformation);
			if($rowFetchMovieInformation['movie_status'] == '0'){
				$movieStatusIsChecked = FALSE;
			}
			$frmActValue = 'doMovieUpdate';
			$xtpl->assign('movie_name', $rowFetchMovieInformation['movie_name']);
			$xtpl->assign('movie_description', $rowFetchMovieInformation['movie_description']);
			$xtpl->assign('movie_price', $rowFetchMovieInformation['movie_price']);
			$xtpl->assign('movie_trailer', $rowFetchMovieInformation['movie_trailer']);
			$xtpl->assign('editing_movie_id', $_GET['movie_id']);
		}
	}
	
	//-- List cate
	$rsListMovieTaskCate = mysql_query("SELECT cate_id, cate_name FROM category WHERE cate_status = '1'");
	while($rowListMovieTaskCate = mysql_fetch_array($rsListMovieTaskCate)){
		if(isset($rowFetchMovieInformation['cate_id']) && $rowFetchMovieInformation['cate_id'] == $rowListMovieTaskCate['cate_id']){
			$rowListMovieTaskCate['thisCateChecked'] = 'selected="selected"';
		}else{
			$rowListMovieTaskCate['thisCateChecked'] = '';
		}
		
		$xtpl->assign('ListMovieTaskCate', $rowListMovieTaskCate);
		$xtpl->parse('center.ListMovieTaskCate');
	}
	
	//--
	if($movieStatusIsChecked == true){
		$xtpl->assign('movieStatusIsChecked', 'checked="checked"');
	}
	$xtpl->assign('frmActValue', $frmActValue);
	
	//-- Save
	if(isset($_POST['frmAct'])){
		if($_POST['frmAct'] == 'doMovieSave'){
			if(checkMovieName($_POST['movie_name'], '') == true){
				mysql_query("INSERT INTO movie (movie_name, cate_id, movie_description, movie_price, movie_status, movie_trailer) VALUES ('".addslashes($_POST['movie_name'])."', '".$_POST['cate_id']."', ('".addslashes($_POST['movie_description'])."'), ('".addslashes($_POST['movie_price'])."'), '".$_POST['movie_status']."', ('".addslashes($_POST['movie_trailer'])."'))");
				
				if(isset($_FILES["movie_image"]["name"]) && $_FILES["movie_image"]["name"] != ''){
					$UL = new UPLOAD();
					$movie_image = $UL->doUploadImage("upload/", "movie_image", '', '110', 'off');
					mysql_query("UPDATE movie SET movie_image = '".$movie_image."' WHERE movie_id = '".mysql_insert_id()."'");
				}
				
				header("Location: ?mod=admin&act=movies");
			}else{
				$xtpl->parse('center.movie_name_existed');
			}
		}
		if($_POST['frmAct'] == 'doMovieUpdate'){
			if(checkMovieName($_POST['movie_name'], $_POST['movie_id']) == true){
				mysql_query("UPDATE movie SET movie_name = '".addslashes($_POST['movie_name'])."', cate_id = '".$_POST['cate_id']."', movie_description = '".addslashes($_POST['movie_description'])."', movie_price = '".$_POST['movie_price']."', movie_status = '".$_POST['movie_status']."', movie_trailer = '".addslashes($_POST['movie_trailer'])."' WHERE movie_id = '".$_POST['movie_id']."'");
				
				if(isset($_FILES["movie_image"]["name"]) && $_FILES["movie_image"]["name"] != ''){
					$UL = new UPLOAD();
					$movie_image = $UL->doUploadImage("upload/", "movie_image", '', '110', 'off');
					mysql_query("UPDATE movie SET movie_image = '".$movie_image."' WHERE movie_id = '".$_POST['movie_id']."'");
				}
				
				header("Location: ?mod=admin&act=movies");
			}else{
				$xtpl->parse('center.movie_name_existed');
			}
		}
	}
	
	function checkMovieName($movieName, $movie_id){
		if($movie_id != ''){
			$rs = mysql_query("SELECT movie_name FROM movie WHERE movie_name = '".addslashes($movieName)."' AND movie_id <> '".addslashes($movie_id)."'");
		}else{
			$rs = mysql_query("SELECT movie_name FROM movie WHERE movie_name = '".addslashes($movieName)."'");
		}
		
		if(mysql_num_rows($rs) > 0){
			return false;
		}else{
			return true;
		}
	}
?>